﻿using Microsoft.EntityFrameworkCore;
using SJ.Platform.Common;
using SJ.Platform.EFCore.Model;
using SJ.Platform.EFCore.Model.FlowManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SJ.Platform.EFCore.DBOperation.DBOperation.MySQL.Flow
{
    public class Flow_Node_ApproveUserHandle
    {
        private readonly SJDBContext _context;

        public Flow_Node_ApproveUserHandle(SJDBContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> CreateAsync(List<FlowNodeApproveUser> entityList)
        {
            try
            {
                int result = 0;
                if (entityList != null && entityList.Count > 0)
                {
                    foreach (FlowNodeApproveUser entity in entityList)
                    {
                        _context.FlowNodeApproveUsers.Add(entity);
                    }
                    result = await _context.SaveChangesAsync();
                }

                return result > 0;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_Node_ApproveUserHandle=>Create", ex);
                return false;
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(FlowNodeApproveUser entity)
        {
            try
            {
                _context.Entry(entity).State = EntityState.Modified;
                await _context.SaveChangesAsync();
                return true;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_Node_ApproveUserHandle=>Update", ex);
                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(FlowNodeApproveUser entity)
        {
            try
            {
                _context.FlowNodeApproveUsers.Remove(entity);
                return _context.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_Node_ApproveUserHandle=>Delete", ex);
                return false;
            }
        }

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteByIdAsync(string id)
        {
            try
            {
                var entity = _context.FlowNodeApproveUsers.Find(id);
                if (entity != null)
                {
                    _context.FlowNodeApproveUsers.Remove(entity);
                    await _context.SaveChangesAsync();
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_Node_ApproveUserHandle=>DeleteById", ex);
            }
            return false;
        }

        /// <summary>
        /// 根据节点ID删除
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteByNodeIdAsync(string nodeId)
        {
            try
            {
                var stakeholders = await _context.FlowNodeApproveUsers.Where(n => n.NodeId == nodeId).ToListAsync();
                int count = _context.Database.ExecuteSqlRaw($"delete from flow_node_approveuser where NodeId='{nodeId}';");
                return stakeholders.Count == count;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_Node_ApproveUserHandle=>DeleteByNodeIdAsync", ex);
            }
            return false;
        }

        /// <summary>
        /// 根据ID获取实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<FlowNodeApproveUser> GetEntityByIdAsync(string id)
        {
            try
            {
                return await _context.FlowNodeApproveUsers.FindAsync(id);
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_Node_ApproveUserHandle=>GetEntityById", ex);
            }
            return null;
        }

        /// <summary>
        /// 根据ID获取实体
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        public async Task<List<FlowNodeApproveUser>> GetEntityListByNodeIdAsync(string nodeId)
        {
            try
            {
                return await _context.FlowNodeApproveUsers.Where(n => n.NodeId == nodeId).ToListAsync();
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_Node_ApproveUserHandle=>GetEntityListByNodeIdAsync", ex);
            }
            return null;
        }
    }
}
